<!-- Dom模板 -->
<template>
  <div class="loginBox">
    <!-- Dom内容 -->
    <div class="pictureBox">
      <img src="../../../public/img/login.png" alt="" />
    </div>
    <div class="loginFrom">
      <div>
        <img src="../../../public/img/login1.png" alt="" />
      </div>
      <div>
        <h2>欢迎登录教育后台系统</h2>
        <p style="margin-left: 20px; color: #c7c7ce">这是一款很好的教育平台</p>
        <div>
          <p>
            <label
              >账号：<el-input
                class="loginUser"
                placeholder="请输入账号"
                v-model="user"
              ></el-input
            ></label>
          </p>
          <p>
            <label
              >密码：<el-input
                class="loginUser"
                placeholder="请输入密码"
                v-model="pass"
                show-password
              ></el-input
            ></label>
          </p>
          <p>
            <input
              type="radio"
              @click="rememberUser"
              :checked="remember"
            />记住用户名
          </p>
          <p>
            <el-button
              @click="jumpHome"
              type="primary"
              style="width: 100%; margin-top: 10px"
              round
              >登录</el-button
            >
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import a from './a'; // 引入组件
export default {
  name: "",
  data() {
    return {
      msg: "测试",
      user: "",
      pass: "",
      remember: false,
      userData: [],
    };
  },
  // 生命周期 - 创建完成（访问当前this实例）
  created() {},
  // 生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.getUserData();
    this.clearValue();
  },
  // Vue方法定义
  methods: {
    async getUserData() {
      let data = await this.$axios.get("/api/users/getUser");
      this.userData = data.data[0];
      console.log(this.userData.username);
    },
    // 清空
    clearValue() {
      let users = JSON.parse(localStorage.getItem("user"));
      if (users) {
        this.user = users.user;
        this.pass = users.pass;
        this.remember = users.isChecked;
      } else {
        this.user = "";
        this.pass = "";
      }
    },
    // 跳转首页
    jumpHome() {
      // 如果账号和密码都存在，那么就跳转页面，并在localhost存值
      if (
        this.user === this.userData.user &&
        this.pass === this.userData.password
      ) {
        if (this.remember === true) {
          let obj = {
            user: this.user,
            pass: this.pass,
            isChecked: this.remember,
            users:this.userData.username,img:this.userData.img
          };
          localStorage.setItem("token", this.user);
          localStorage.setItem("user", JSON.stringify(obj));
        }
        let obj = {user:this.userData.username,img:this.userData.img}
        let parObj = JSON.stringify(obj)
        this.$router.push({
          path:'/firstPage',
          query:{
            params:parObj
          }
        });
      } else {
        this.$message({
          showClose: true,
          message: "请输入账号和密码",
          type: "error",
        });
      }
      let users = JSON.parse(localStorage.getItem("user")) ? JSON.parse(localStorage.getItem('user')) : {};
      if (this.user === users.user && this.pass === users.pass) {
        let obj = {user:this.userData.username,img:this.userData.img}
        let parObj = JSON.stringify(obj)
        this.$router.push({
          path:'/firstPage',
          query:{
            params:parObj
          }
        });
      }
    },
    // 记住用户
    rememberUser() {
      this.remember = !this.remember;
      if (this.remember === false) {
        localStorage.removeItem("user");
      }
    },
  },
};
</script>

<style lang="less" scoped>
/* @import url(''); 引入css类 */
.loginBox {
  width: 100%;
  height: 100vh;
  > .pictureBox {
    width: 100%;
    height: 100%;
    position: relative;
    > img {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }
  }
  .loginFrom {
    width: 840px;
    height: 479px;
    background: #fff;
    border-radius: 10px;
    box-shadow: 2px 0px 20px 0px rgba(0, 0, 0, 0.4);
    position: absolute;
    top: calc(50% - 239px);
    left: calc(50% - 420px);
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    .loginUser {
      width: 75%;
      margin: 10px 0px;
    }
  }
}
</style>